

Ext.namespace('Ext.expadminExpData');
// Ext.expadminExpData.employDepart = [['0', '财务部'], ['1', '人力资源部'], ['2',
// '技术部'],
// ['3', '后勤部']];
Ext.expadminExpData.employStatus = [['4', '全部'], ['0', '在职'], ['1', '办理离职中'],
		['2', '已离职']];

var empImportwin;
var empRolllWin;
var empStore;
Ext.onReady(function() {
	empImportwin = interfaceImport(1013);
	win = addEmployeeInfo();
	empRolllWin = empRoll();

	var displayStatusName = ['在职', '办理离职中', '已离职'];

	var contenttreecombo = comboxTree();

	// 状态的下拉框
	var employStatusCombox = new Ext.form.ComboBox({
				fieldLabel : '状态',
				hiddenName : 'employStatus',
				store : new Ext.data.SimpleStore({
							fields : ['abbr', 'employStatus'],
							data : Ext.expadminExpData.employStatus
						}),
				displayField : 'employStatus',
				typeAhead : true,
				valueField : 'abbr',
				mode : 'local',
				readOnly : true,
				triggerAction : 'all',
				emptyText : '请选择...',
				selectOnFocus : true,
				anchor : '90%',
				width : 150
			});

	// 日期控件
	Ext.apply(Ext.form.VTypes, {
		daterange : function(val, field) {
			var date = field.parseDate(val);
			if (!date) {
				return;
			}
			if (field.startDateField
					&& (!this.dateRangeMax || (date.getTime() != this.dateRangeMax
							.getTime()))) {
				var start = Ext.getCmp(field.startDateField);
				start.setMaxValue(date);
				start.validate();
				this.dateRangeMax = date;
			} else if (field.endDateField
					&& (!this.dateRangeMin || (date.getTime() != this.dateRangeMin
							.getTime()))) {
				var end = Ext.getCmp(field.endDateField);
				end.setMinValue(date);
				end.validate();
				this.dateRangeMin = date;
			}
			return true;
		}
	});

	var searchPanel = new Ext.form.FormPanel({
		frame : true,
		buttonAlign : 'right',
		title : "雇员信息维护",
		id : 'employMessage',
		bodyStyle : 'padding:5px 5px 0',
		url : '',
		items : [{
			layout : 'column',
			items : [{
						columnWidth : .5,
						layout : 'form',
						items : [{
									xtype : 'textfield',
									fieldLabel : '工号',
									name : 'emp_cs_code',
									id : 'emp_cs_code',
									anchor : '90%',
									emptyText : '请输入工号'
								}, {
									layout : 'form',
									items : [contenttreecombo]
								}, {
									fieldLabel : '入职日期从',
									anchor : '90%',
									name : 'startdt',
									id : 'startdt',
									xtype : 'datefield',
									vtype : 'daterange',
									readOnly : true,
									endDateField : 'enddt'
								}]
					}, {
						columnWidth : .5,
						layout : 'form',
						items : [{
									fieldLabel : '姓名',
									anchor : '90%',
									name : 'name',
									id : 'name',
									emptyText : '请输入工号',
									xtype : 'textfield'
								}, {
									columnWidth : .5,
									layout : 'form',
									items : [employStatusCombox]

								}, {
									fieldLabel : '入职时期到',
									anchor : '90%',
									name : 'enddt',
									id : 'enddt',
									xtype : 'datefield',
									vtype : 'daterange',
									readOnly : true,
									startDateField : 'startdt'
								}]
					}],
			buttons : [{
						text : '新增',
						id : '_btnSearch',
						handler : function() {
							Ext.getCmp('addemployeeWin').setTitle('新增雇员信息');
							win.show();
						}
					}, {
						text : '查询',
						id : '_btnSearch',
						handler : function() {
							var sd = searchPanel.findById('startdt').getValue();
							if (sd != null && sd != "") {
								sd = sd.format('Y-m-d');
							} else {
								sd = null;
							}
							var ed = searchPanel.findById('enddt').getValue();
							if (ed != null && ed != "") {
								ed = ed.format('Y-m-d');
							} else {
								ed = null;
							}
							empStore.on('beforeload', function() {
								empStore.baseParams = {
									emp_cs_code : searchPanel
											.findById('emp_cs_code').getValue(),
									name : searchPanel.findById('name')
											.getValue(),
									orgchat_id : contenttreecombo.passField.value,
									status : employStatusCombox.getValue(),
									startdt : sd,
									enddt : ed
								}
							});
							empStore.reload();
						}
					}, {
						text : '删除',
						id : '_btnSearch',
						handler : function() {
							var length = accountGrid.getSelectionModel()
									.getSelections().length
							if (length < 1) {
								Ext.Msg.alert('警告', '请选择需要删除的雇员');
								return;
							}
							Ext.MessageBox.show({
								title : '提示',
								msg : '您选中了' + length + '条记录进行删除,请问您确认吗?',
								buttons : Ext.MessageBox.OKCANCEL,
								fn : function(btn) {
									if (btn == "ok") {

										var deletePbEmployId = [];
										var rs = accountGrid
												.getSelectionModel()
												.getSelections();
										for (var i = 0; i < length; i++) {
											var employeeId = rs[i].data.employee_id;
											deletePbEmployId.push(employeeId);

										}
										alert(deletePbEmployId);
										Ext.Ajax.request({
											url : "GetAllPbEmploy!deletePbEmployee.action?employee_id="
													+ Ext
															.encode(deletePbEmployId),
											method : "post",
											success : function(response,
													options) {
												empStore.reload();
												Ext.Msg.alert('提示', '删除操作成功！');
											}
										});

									}
									if (btn == "cancel") {
										return;
									}
								}
							});

						}
					}, {
						text : '雇员信息导入',
						id : '_btnSearch',
						handler : function() {
							empImportwin.show();

						}
					}]
		}]
	});

	searchPanel.render(Ext.getBody());

	// 数据源
	empStore = new Ext.data.Store({
				url : 'GetAllPbEmploy!queryAllEmpAdmin.action',
				reader : new Ext.data.JsonReader({
							root : 'result.result',
							totalProperty : 'result.totalCount'
						}, [{
									name : 'emp_cs_code',
									mapping : 'emp_cs_code'
								}, {
									name : 'name',
									mapping : 'name'
								}, {
									name : 'employee_id',
									mapping : 'employee_id'
								}, {
									name : 'join_date',
									mapping : 'join_date'
								}, {
									name : 'out_jobdate',
									mapping : 'out_jobdate'
								}, {
									name : 'is_valid',
									mapping : 'is_valid'
								}, {
									name : 'is_injob',
									mapping : 'is_injob'
								}, {
									name : 'login_code',
									mapping : 'login_code'
								}, {
									name : 'orgchart_name',
									mapping : 'orgchart_name'
								}])
			});

	// 查询显示的表格
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
				header : '员工编号',
				dataIndex : 'emp_cs_code',
				width : 100
			}, {
				header : '姓名',
				dataIndex : 'name',
				width : 100
			}, {
				header : '部门',
				dataIndex : 'orgchart_name',
				width : 160
			}, {
				header : '入职日期',
				dataIndex : 'join_date',
				width : 100,
				renderer : function(v, p, r) {
					if (v != null) {
						return v.substring(0, 10);
					}
				}
			}, {
				header : '状态',
				dataIndex : 'is_valid',
				width : 100,
				renderer : function(value, p, record) {
					if (value == 1 && record.data.out_jobdate != null) {
						return displayStatusName[1];
					} else if (value == 0 && record.data.out_jobdate != null) {
						return displayStatusName[2];
					} else {
						return displayStatusName[0];
					}
				}
			}, {
				header : '分配权限',
				dataIndex : 'login_code',
				width : 150,
				renderer : function(value, p, record) {
					if (value != null) {
						return "<input type='button' class='btn' value='查看权限' onclick=\"empRollSelect("
								+ record.data.employee_id
								+ ",'"
								+ record.data.name + "' )\">";
					} else if (value == null && record.data.is_valid == 0
							&& record.data.out_jobdate != null) {
						return "<input type='button' class='btn' id='empRollButton' value='分配权限' disabled='disabled' >";
					} else {
						return "<input type='button' class='btn' id='empRollButton' value='分配权限' onclick=\"empRollShow("
								+ record.data.employee_id
								+ ",'"
								+ record.data.name + "' )\">";
					}
				}
			}]);

	var accountGrid = new Ext.grid.GridPanel({
				height : document.body.clientHeight
						- Ext.getCmp('employMessage').getInnerHeight()
						- Ext.getCmp('employMessage').getFrameHeight(),
				id : 'pbEmp',
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : empStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
				sm : sm,
				cm : cm,
				store : empStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});

	accountGrid.render(Ext.getBody());

	empStore.load();

	accountGrid.addListener('rowdblclick', rowdblclickFn);

});

function rowdblclickFn(grid, rowIndex, e) {

	Ext.Ajax.request({
		url : 'GetAllPbEmploy!queryEmployeeInfo.action',
		method : 'post',
		params : {
			employee_id : grid.getStore().getAt(rowIndex).data.employee_id
		},
		success : function(response, options) {
			Ext.getCmp('addemployeeWin').setTitle('修改雇员信息');
			win.show();
			var responseTextValue = Ext.util.JSON.decode(response.responseText);
			var tabPanel = win.findById('employeeInfoPanel');
			// baseInfo
			var baseInfoJSON = responseTextValue.employeeInfo.pbEmployee;
			tabPanel.findById('baseInfoPanelForm').findById('employee_id')
					.setValue(baseInfoJSON.employee_id);
			tabPanel.findById('baseInfoPanelForm').findById('emp_code')
					.setValue(baseInfoJSON.emp_cs_code);
			tabPanel.findById('baseInfoPanelForm').findById('emp_name')
					.setValue(baseInfoJSON.name);
			tabPanel.findById('baseInfoPanelForm').findById('birthday')
					.setValue(getDate(baseInfoJSON.birthday,
							'yyyy-MM-ddTHH:mm:ss'));
			tabPanel.findById('baseInfoPanelForm').findById('id_card_type')
					.setValue(baseInfoJSON.id_card_type);
			tabPanel.findById('baseInfoPanelForm').findById('gender')
					.setValue(baseInfoJSON.gender);
			tabPanel.findById('baseInfoPanelForm').findById('nationality')
					.setValue(baseInfoJSON.nationality);
			tabPanel.findById('baseInfoPanelForm').findById('id_card_num')
					.setValue(baseInfoJSON.id_card_num);
			tabPanel.findById('baseInfoPanelForm').findById('resident_address')
					.setValue(baseInfoJSON.resident_address);
			tabPanel.findById('baseInfoPanelForm').findById('address')
					.setValue(baseInfoJSON.address);
			tabPanel.findById('baseInfoPanelForm').findById('postcode')
					.setValue(baseInfoJSON.postcode);
			tabPanel.findById('baseInfoPanelForm').findById('resident_type')
					.setValue(baseInfoJSON.resident_type);
			tabPanel.findById('baseInfoPanelForm').findById('marriage_status')
					.setValue(baseInfoJSON.marriage_status);
			tabPanel.findById('baseInfoPanelForm').findById('contact_tel1')
					.setValue(baseInfoJSON.contact_tel1);
			tabPanel.findById('baseInfoPanelForm').findById('contact_tel3')
					.setValue(baseInfoJSON.contact_tel3);
			tabPanel.findById('baseInfoPanelForm').findById('education_level')
					.setValue(baseInfoJSON.education_level);
			tabPanel.findById('baseInfoPanelForm').findById('email')
					.setValue(baseInfoJSON.email);
			tabPanel.findById('baseInfoPanelForm').findById('contact_tel2')
					.setValue(baseInfoJSON.contact_tel2);
			// tabPanel.findById('baseInfoPanelForm').findById('position')
			// .setValue(baseInfoJSON.position);
			tabPanel.findById('baseInfoPanelForm').findById('base_pay_pro')
					.setValue(baseInfoJSON.base_pay_pro);
			tabPanel.findById('baseInfoPanelForm').findById('join_date')
					.setValue(getDate(baseInfoJSON.join_date,
							'yyyy-MM-ddTHH:mm:ss'));
			tabPanel.findById('baseInfoPanelForm').findById('in_jobdate')
					.setValue(getDate(baseInfoJSON.in_jobdate,
							'yyyy-MM-ddTHH:mm:ss'));
			// tabPanel.findById('baseInfoPanelForm').findById('department').setValue(baseInfoJSON.department);
			tabPanel.findById('baseInfoPanelForm').findById('service_start')
					.setValue(baseInfoJSON.service_start);
			tabPanel.findById('baseInfoPanelForm').findById('out_jobdate')
					.setValue(getDate(baseInfoJSON.out_jobdate,
							'yyyy-MM-ddTHH:mm:ss'));
			tabPanel.findById('baseInfoPanelForm').findById('health_status')
					.setValue(baseInfoJSON.health_status);
			tabPanel.findById('baseInfoPanelForm').findById('base_pay')
					.setValue(baseInfoJSON.base_pay);
			tabPanel.findById('baseInfoPanelForm').findById('contract_type')
					.setValue(baseInfoJSON.contract_type);
			tabPanel.findById('baseInfoPanelForm').findById('polity')
					.setValue(baseInfoJSON.polity);

			// 部门
			var departmentJSON = responseTextValue.employeeInfo.departmentName;
			tabPanel.findById('baseInfoPanelForm').findById('department')
					.setValue(departmentJSON);
			if (responseTextValue.employeeInfo.csOrgEmp != null
					&& responseTextValue.employeeInfo.csOrgEmp.orgchart_id != null) {
				tabPanel.findById('baseInfoPanelForm').findById('department').passField.value = responseTextValue.employeeInfo.csOrgEmp.orgchart_id;
			}

			// 岗位
			// var positionJSON = responseTextValue.employeeInfo.csPositionEmp;
			// tabPanel.findById('baseInfoPanelForm').findById('position')
			// .setValue(positionJSON.position_code);

			// 社保卡账号
			if (responseTextValue.employeeInfo.pbEmpSocCard != null
					&& responseTextValue.employeeInfo.pbEmpSocCard.soccard_code != null) {
				tabPanel
						.findById('socialSecurityNumberPanel')
						.findById('socialSecurityNumber')
						.setValue(responseTextValue.employeeInfo.pbEmpSocCard.soccard_code);
			}
			// 公积金账号
			if (responseTextValue.employeeInfo.pbEmpAccfundcard != null
					&& responseTextValue.employeeInfo.pbEmpAccfundcard.accfundcard_code != null) {
				tabPanel
						.findById('socialSecurityNumberPanel')
						.findById('surplusAccount')
						.setValue(responseTextValue.employeeInfo.pbEmpAccfundcard.accfundcard_code);
			}
			// 银行卡
			var cardsJSON = responseTextValue.employeeInfo;
			if (cardsJSON != null) {
				tabPanel.findById('bankCardNumberPanel')
						.findById('bankCardGrid').getStore().loadData(
								cardsJSON, false);
			}
		},
		failure : function(response, options) {
			var responseTextVale = Ext.util.JSON.decode(response.responseText);
			Ext.Msg.alert('警告', responseTextVale.msg);
		}
	});
}
function empRollShow(id, name) {
	Ext.getCmp('_name').setValue(name);
	Ext.getCmp('_employeeId').setValue(id);
	Ext.getCmp('login_code').setValue('');
	Ext.getCmp('pwd').setValue('');
	Ext.getCmp('repwd').setValue('');
	Ext.getCmp('checkButton').setDisabled(false);
	Ext.getCmp('updateOrInsert').setValue('insert');
	isUsed = '';
	empRolllWin.show();
}

function empRollSelect(id, name) {
	empRolllWin.findById('_employeeId').setValue(id);
	empRolllWin.findById('_name').setValue(name);
	Ext.getCmp('updateOrInsert').setValue('update');
	isUsed = '';
	Ext.Ajax.request({
		url : 'GetAllPbEmploy!queryEmpRoll.action',
		method : 'post',
		params : {
			empId : id
		},
		success : function(response, result) {
			var responseTextValue = Ext.util.JSON.decode(response.responseText);
			Ext.getCmp('login_code').setValue(responseTextValue.msg);
			Ext.getCmp('login_code').setDisabled(true);
			Ext.getCmp('pwd').setDisabled(true);
			Ext.getCmp('pwd').setValue('password');
			Ext.getCmp('repwd').disable();
			Ext.getCmp('repwd').show();
			Ext.getCmp('repwd').setValue('password');
			Ext.getCmp('checkButton').setDisabled(true);
			var str = '';
			for (var index = 0; index < responseTextValue.list.length; index++) {
				str += responseTextValue.list[index].id;
				str += ',';
			}
			Ext.getCmp('multiselect').setValue(str.substring(0, str.length - 1)
					.toString());
			empRolllWin.show();
		},
		failure : function() {
			Ext.Msg.show({
						title : '操作信息',
						msg : '操作失败，请联系管理员',
						buttions : Ext.Msg.OK
					});
		}
	});
	empRolllWin.show();
}